﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;

namespace CNRS.ldapAS
{
    class Program
    {
        static void Main(string[] args)
        {

            Dictionary<string, string> config = new Dictionary<string, string>();
            config.Add("servername", ConfigurationManager.AppSettings["servername"]);
            config.Add("port", ConfigurationManager.AppSettings["port"]);
            config.Add("username", ConfigurationManager.AppSettings["username"]);
            config.Add("password", ConfigurationManager.AppSettings["password"]);
            config.Add("secured", ConfigurationManager.AppSettings["secured"]);
            config.Add("root", ConfigurationManager.AppSettings["root"]);

            CNRSldapAS attributeStore = new CNRSldapAS();
            attributeStore.Initialize(config);

            // to allow multiple calls, just read key for input. We quit with ctrl+C or quit
            while (true)
            {
                Console.Write("Enter the user ePPN : ");
                string input = Console.ReadLine();

                if (!string.IsNullOrEmpty(input))
                {
                    if (input.ToLower() == "quit")
                    {
                        break;
                    }
                    else
                    {
                        // check if the input is in a good form

                        // call the attribute store : test user scott_koranda_uwm@unitedid.org
                        IAsyncResult result = attributeStore.BeginExecuteQuery(ConfigurationManager.AppSettings["query"], new string[] { input }, null, null);

                        // Transform the result returned by the attribute store
                        string[][] data = attributeStore.EndExecuteQuery(result);
                        foreach (string[] row in data)
                        {
                            foreach (string col in row)
                            {
                                Console.Write("{0}\t\t", col);
                            }
                            Console.WriteLine();
                        }
                    }
                }
            }
        }
    }
}
